import pymssql
import config

server = '172.16.3.19'
user = 'sa'
password = '*#zw8'
database = 'TOP_ERP_Base'

try:

    # 打开数据库连接（ip/数据库用户名/登录密码/数据库名）
    db = pymssql.connect(server=server, user=user, password=password, database=database)
    print("数据库连接成功")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
except pymssql.Error as e:
    print("数据库连接失败：" + str(e))


# 导入数据到数据库中
def InsertBatchToDB(result):
    del_sql = "delete from dbo.nTB_ArrClassPreNewAutoSd_info"
    int_sql = "insert into dbo.nTB_ArrClassPreNewAutoSd_info values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    # sql = "insert into dbo.testInsert values ('wang',12,'男')"
    # sql = "insert into dbo.testInsert values (%s,%s,%s)"
    try:
        cursor.execute(del_sql)
        cursor.executemany(int_sql, result)
        db.commit()
    except Exception as e:
        print(e)
        db.rollback()
    finally:
        db.close()
